package client.presenter;

import java.util.Observable;
import java.util.Observer;

import client.model.Model;
import client.view.View;

public class Presenter implements Observer {
	View view;
	Model model;
	
	public Presenter(Model model, View view) {
		this.model = model;
		this.view = view;
	}

	public void update(Observable observable, Object arg1) {
		if (observable == view) {
			int input = view.getUserCommand();
			
			switch (input) {
				case 1:
					model.moveUp();
					break;
				case 2:
					model.moveDown();
					break;
				case 3:
					model.moveLeft();
					break;
				case 4:
					model.moveRight();
					break;
				case 5:
					model.moveUpRight();
					break;
				case 6:
					model.moveUpLeft();
					break;
				case 7:
					model.moveDownRight();
					break;
				case 8:
					model.moveDownLeft();
					break;
				default:
					break;
			}
		}	
		else if (observable == model) {
			view.setLockControls(false);
			view.updateDisplay(model.getBoard(), model.getScore());
			
			switch (model.getGameState()) {
				// Lost game
				case 1:
					view.showEndGame(model.getGameState());
					view.setLockControls(true);
					break;
				// Won game, with options
				case 2:
					boolean userChoise = view.showEndGame(model.getGameState());
					
					if (userChoise) {
						view.setLockControls(true);
					}
					else {
						model.userOption();
					}
					
					break;
				// Won game, no options
				case 3:
					view.showEndGame(model.getGameState());
					view.setLockControls(true);
					break;
				default:
					break;
			}
		}
	}	
}
